//  this library.
import 'dart:convert';

import 'package:flutter_hi_cache/flutter_hi_cache.dart';
import 'package:hktk_app/model/home/exam_key_model.dart';
import 'package:hktk_app/model/industry/exam_model.dart';
import 'package:hktk_app/model/industry/industry_model.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/util/dao_util.dart';
import 'package:hktk_app/util/my_util.dart';

/// 获取行业相关的接口
class IndustryDao {
  /// 科目缓存相关的key值
  static const String _subjectKey = "subject-key";

  /// 选中科目下标
  static const String _selectSubId = "select-sub-id";

  /// 考试缓存相关信息
  static const String _examKey = "exam-key";
  /// 考试缓存相关信息
  static const String _industry_code = "industry-code";

  /// 获取相关行业信息请求
  static Future<List<IndustryModel>> getIndustry() async {
    String url = "/exam/industry/app/list?groupId=${getGroupId()}";
    var data = await httpGET(url: url, tips: "获取行业");
    if (data is List<dynamic>) {
      return data.map((dynamic map) => IndustryModel.fromJson(map)).toList();
    } else {
      return <IndustryModel>[];
    }
  }

  /// 根据行业获取考试
  static Future<List<ExamModel>> examByIndustryId(String industryId) async {
    String url =
        "/exam/exam/exam-by-industryId-app?size=1000&industryId=$industryId";
    // 发送请求
    var data = await httpGET(url: url, tips: "根据行业获取考试");
    if (data is List<dynamic>) {
      return data.map((dynamic map) => ExamModel.fromJson(map)).toList();
    } else {
      return <ExamModel>[];
    }
  }

  /// 获取考试下科目接口
  static Future<List<CategoryModel>?> leafList({
    required String examId,
    int parentId = 0,
  }) async {
    String url = "/exam/category/leaf-list?examId=$examId&parentId=$parentId";
    // 发送请求
    var data = await httpGET(url: url, tips: "获取考试下科目");
    if (data is List<dynamic>) {
      return data.map((dynamic map) => CategoryModel.fromJson(map)).toList();
    } else {
      return null;
    }
  }

  /// 设置用户选择的科目
  static void settingChoose({
    required List<CategoryModel> subjectList,
    int index = 0,
  }) async {
    // 计算数据
    List<Map<String, dynamic>> list = MyUtil.getSelectSubject(
      subjectList: subjectList,
      index: index,
    );
    // 封装条件
    Map<String, dynamic> params = {
      "categoryList": list,
    };
    String url = "/exam/category/setting-choose";
    await httpPOST(url: url, params: params, tips: "设置用户选择的科目");
  }

  /// 缓存类目相关数据
  static void saveCategoryModels(List<CategoryModel> models) {
    String encode = jsonEncode(models);
    HiCache.getInstance().setString(_subjectKey, encode);
  }

  /// 获取缓存的科目
  static List<CategoryModel> getCategoryModels() {
    String? value = HiCache.getInstance().get(_subjectKey);
    if (value == null) return [];
    List<dynamic> decode = jsonDecode(value);
    return decode.map((map) {
      return CategoryModel.fromJson(map);
    }).toList();
  }

  /// 获取缓存的已选中科目
  static List<CategoryModel> getCategoryModelsSel() {
    List<CategoryModel> list = getCategoryModels();
    return MyUtil.getSelList(list);
  }

  /// 缓存选中科目下标
  static void saveSelectCategoryId(int subId) {
    HiCache.getInstance().setInt(_selectSubId, subId);
  }

  /// 获取选中的科目下标
  static int? getSelectCategoryId() {
    return HiCache.getInstance().get(_selectSubId);
  }
  // static setIndustryCode(String industryCode){
  //   HiCache.getInstance().setString(_industry_code, industryCode);
  // }
  // static String? getIndustryCode(){
  //   return HiCache.getInstance().get(_industry_code);
  // }
  /// 缓存考试相关信息
  static void saveExamKey(ExamKeyModel examKeyModel) {
    String encode = jsonEncode(examKeyModel);
    HiCache.getInstance().setString(_examKey, encode);
  }

  /// 获取缓存的考试相关信息,
  static ExamKeyModel getExamKey() {
    // 为什么有时获取为null呢
    String value = HiCache.getInstance().get(_examKey);
    dynamic val = jsonDecode(value);
    return ExamKeyModel.fromJson(val);
    // return ExamKeyModel(examId: "1",examName: "考试",industryId: "123",industryName: "行业");
  }
  static bool isInitExamKey(){
    return HiCache.getInstance().get(_examKey) != null;
  }
  static Future<void> submitSuggestion(Map<String, dynamic> params) async {
    String url = "/exam/exam/submit-suggestion";
    await httpPOST(params: params, url: url, tips: "提交意见");
  }
}
